<?php
class ControllerAccountReward extends Controller {
    //增加或消耗积分记录(消耗points 传'-')
    public function updateReward() {
        if (! $this->actionCheck(true, true)) {
            return;
        }
        $this->load->model('account/reward');
        $this->model_account_reward->addReward($this->customer->getId(),$this->request->post['description'],$this->request->post['points'],$this->request->post['order_id']);
        $this->response->setDataResult(1, '成功！', null);
    }

    //获取用户的总积分数
    public function getCutomerRewardTotal(){
        if (! $this->actionCheck(true, false)) {
            return;
        }
        $this->load->model('account/reward');
        $data['total']=$this->model_account_reward->getRewardTotal($this->customer->getId());
        $this->response->setDataResult(1, '成功！', $data);
    }

    //获取用户积分记录
    public function getCustomerRewardRecords(){
        if (! $this->actionCheck(true, false)) {
            return;
        }

        $page = isset($this->request->get['page'])?$this->request->get['page']:1;

        $limit=isset($this->request->get['limit'])?$this->request->get['limit']:10;

        $this->load->model('account/reward');
        $rewards['reward'] = array();
        $results=$this->model_account_reward->getRewards($this->customer->getId(),($page - 1)*$limit,$limit);

        foreach ($results as $result) {
            $rewards['reward'][] = array(
                'id' => $result['customer_reward_id'],
                'customer_id'     => $result['customer_id'],
                'order_id'       => $result['order_id'],
                'description'     => $result['description'],
                'points' => $result['points'],
                'date_added' => date($this->config->get('date_format_long'), strtotime($result['date_added']))
            );
        }
        $rewards['pageIndex'] = $page;
        $rewards['pageSize'] = $limit;
        $rewards['total']=$this->model_account_reward->getTotalRewards($this->customer->getId());

        $this->response->setDataResult(1, '成功！', $rewards);

    }
}